package LawsonTransformJava.xref;

import java.sql.SQLException;

public class XrefLPTranType extends CrossRef {

	private final static String TABLE_NAME = "xLPTRANTYPE";
	static final String XLATE_ERROR = "LP_ERROR";

	public XrefLPTranType() throws SQLException {
		stmt = xref.getDataStatement();
		rslt = stmt.executeQuery("SELECT * FROM " + TABLE_NAME);
		super.XLATE_ERROR=XLATE_ERROR;
		while (rslt.next()) {
			// The following should get the appropriate column from the Xref Table
			String old = rslt.getString(1);
			String newX = rslt.getString(2);
			values.put(old, newX);
		}
	}

	@Override
	public String xlate(String lookup) {
		//Reset errFound
		errFound = false;
		//Strip out Quotes
		String strip = lookup.replaceAll("\"", "");
		//If lookup value is blank coming in, then pass it back out without a lookup
		if (strip.equals(""))
			return strip;

		//Value found
		String newX = (String)values.get(String.valueOf(strip));

		//Lookup value; if not found send back 00 if allowed otherwise send XLATE_ERROR and flag errFound
		if (allowBlanks)
			return (newX==null?"00":newX);
		else {
			if (newX==null) {
				errFound = true;
				return XLATE_ERROR + "_" + String.valueOf(lookup.replaceAll("\"", ""));
			} else
			return newX;
		}
	}
}
